package com.ytt.core.service.service.role;

import com.ytt.core.service.entity.role.Role;
import com.ytt.core.service.exception.NoSuchDataException;
import com.ytt.core.service.repository.RoleRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

/**
 * RoleImpl
 *
 * @author chenwen
 * @date 2019/9/17 11:25
 */
@Slf4j
@Service("roleService")
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class RoleImpl implements RoleService {
    
    private  final RoleRepository roleRepository;

    public RoleImpl(RoleRepository roleRepository) {
        this.roleRepository = roleRepository;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Set<String> getRoleAuthorities(Long id) {
        Optional<Role> role = roleRepository.findById(id);
        if (!role.isPresent()) {
            throw new NoSuchDataException(id.toString());
        }
        Set<String> authorities = new HashSet<>();
        role.get().getAuthorities().forEach(rr -> authorities.add(rr.getCode()));
        return authorities;
    }
}
